<template>
  <div id="main">
    <!-- 顶部标题 -->
    <div class="title-infomation">
      <h2>成绩复核</h2>
    </div>
    <!-- 功能区 -->
    <div class="table-allinfomation">
      <div id="table-info">
        <!-- 条件搜索 -->
        <div class="top-infomation">

          <div class="search-info">
            <el-form v-model="searchUserInfo" :inline="true" class="search-infomation">
              <el-form-item label="根据姓名查询信息">
                <el-input
                  v-model="searchUserInfo"
                  clearable
                  maxlength="5"
                  show-word-limit
                  placeholder="请输入姓名"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="searchUserInfomation">查询</el-button>
                <el-button plain @click="cleanSearchUserInfomation">重置</el-button>
              </el-form-item>
            </el-form>
          </div>

        <!-- 搜索按钮 -->
        </div>
        <!-- 表单展示 -->
        <el-table
          ref="userInfoTable"
          v-loading="loading"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          :data="userInfo.slice((currentPage-1)*pagesize,currentPage*pagesize)"
          stripe
          fit
          highlight-current-row
          class="table-infomation"
        >
          <el-table-column
            label="序号"
            type="index"
            min-width="12%"
            align="center"
          >
            <template slot-scope="scope">
              <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="userId"
            label="用户工号"
            sortable
            :sort-orders="sortOrders"
            min-width="12%"
          />
          <el-table-column
            align="center"
            prop="userinfo.userName"
            label="用户姓名"
            min-width="12%"
          />
          <el-table-column
            align="center"
            prop="userinfo.userBranch"
            label="所属党小组"
            min-width="10%"
            :filters="userAllBranch"
            filter-multiple
            column-key="userinfo.userBranch"
            :filter-method="selectUserBranch"
          />
          <el-table-column
            align="center"
            label="当前成绩"
            min-width="12%"
            prop="userinfo.userScore"
            sortable
            :sort-orders="sortOrders"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.userinfo.userScore < -500">一票否决状态</div>
              <div v-if="scope.row.userinfo.userScore > -500">{{ scope.row.userinfo.userScore }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="详情"
            min-width="20%"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button v-if="scope.row.userinfo.userScore < -500" type="text" @click="updateUserVeto(scope.row)">解除一票否决</el-button>
              <el-button v-if="scope.row.userinfo.userScore > -500" type="text" @click="updateUser(scope.row)">成绩复核</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页功能 -->
        <div id="pages" class="paging-info">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[5,10]"
            :page-size="pagesize"
            layout="total , sizes , prev , pager , next"
            :total="userInfo.length"
            prev-text="上一页"
            next-text="下一页"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>

      </div>
    </div>
    <!-- 修改信息 -->
    <review-user ref="reviewUser" />
  </div>
</template>

<script>
import { getAllUserInfo, searchUserInfomations, updateUserStatus, updateUserInfo } from '../../api/user.js'
import { getScoreByUserId, forUserGetSend, getUpdateSendScore } from '../../api/send.js'
import reviewUser from '../../components/reviewUser/reviewUser.vue'
import { getGroups } from '@/api/branch.js'
export default {
  components: {
    'review-user': reviewUser
  },
  data() {
    return {
      // 获取全部数据存储位置
      userInfo: [],
      originUserInfo: [],
      routerInfo: [],
      routerAllId: [],
      sendAllUser: [],
      // 代码数据存储位置
      loading: true,
      userInfoSend: '0',
      currentPage: 1, // 默认显示页面为1
      pagesize: 10, // 每页的数据条数
      sortOrders: [
        'ascending', 'descending'
      ],
      userAllBranch: [],
      // 搜索设置中转值和最后值
      searchUserInfo: '', // 中转值
      searchUserName: '', // 保存值
      showStatus: false
    }
  },
  computed: {
  },
  watch: {
  },
  created() {
    // 获取所有用户信息
    this.getAllUserInfo()
  },
  methods: {
    // 全部调用方法

    // 获取所有用户信息
    getAllUserInfo() {
      var that = this
      new Promise(function(resolve, reject) {
        // 获取所有党小组信息
        getGroups().then((data) => {
          const userAllBranch = []
          for (const i in data.obj) {
            const userBranch = {
              value: data.obj[i].branchName,
              text: data.obj[i].branchName
            }
            userAllBranch.push(userBranch)
          }
          that.userAllBranch = userAllBranch
          getAllUserInfo().then((res) => {
            const userInfo = res.obj
            for (var i = 0; i < userInfo.length; i++) {
              userInfo[i].userinfo.userBranchNum = userInfo[i].userinfo.userBranch
              userInfo[i].userinfo.userBranch = data.obj.find(item => item.branchId === userInfo[i].userinfo.userBranch).branchName
            }
            resolve(userInfo)
          })
        })
      }).then((data) => {
        new Promise(function(resolve, reject) {
          resolve(data)
        }).then((data1) => {
          var data2 = []
          var dataLength = data1.length
          for (var x = 0; x < dataLength; x++) {
            if (data1[x].userinfo.userReconsider === 1 || data1[x].userinfo.userReconsider === '1') {
              data2.push(data[x])
            }
          }
          for (let i = 0; i < data2.length; i++) {
            getScoreByUserId(data2[i].userId).then(res => {
              data2[i].userinfo.userScore = Object.values(res.obj)[0]
            })
          }
          that.findTypeName(data2)
        })
      })
    },
    // 根据userid查send表，计算当前总分
    findTypeName(user) {
      var that = this
      that.userInfo = user
      this.loading = false
    },
    // 修改信息
    updateUser(user) {
      this.$refs.reviewUser.showDialog(user)
    },

    // 解除一票否决
    updateUserVeto(userInfo) {
      // 获取全部数据
      // 根据userInfo.userId查send表
      this.$confirm('当前用户处于一票否决状态，如需取消全部一票否决，请点击确定。', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var sendUpdataScoreId = []
        new Promise(function(resolve, reject) {
          forUserGetSend(userInfo.userId).then((res) => {
            resolve(res.obj)
          })
        }).then((data) => {
          for (let i = 0; i < data.length; i++) {
            if (data[i].sendScore === -999) {
              sendUpdataScoreId.push(data[i].sendId)
            }
          }
          // 根据sendId修改成绩信息
          var updateUserInfoStatus = 0
          for (let j = 0; j < sendUpdataScoreId.length; j++) {
            const send = {
              sendId: '',
              sendGiveUserId: '',
              sendScore: ''
            }
            send.sendId = sendUpdataScoreId[j]
            send.sendGiveUserId = sessionStorage.getItem('userId')
            send.sendScore = 0
            getUpdateSendScore(send, 0).then(res => {
            })
            if (j === sendUpdataScoreId.length - 1) {
              updateUserInfoStatus = 1
            }
          }
          if (updateUserInfoStatus === 1) {
            this.updateUserInfo(userInfo, sendUpdataScoreId.length)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已成功取消'
        })
      })

      // 更新用户总分
    },
    // 更新用户总分
    updateUserInfo(userInfo, vetoLength) {
      // 获取全部数据
      var that = this
      var userBranch = that.userNewBranch
      // 判断分数状态
      var userScore = ''
      // 获取当前分数
      var userAddScore = 0
      for (let i = 0; i < vetoLength; i++) {
        userAddScore += 999
      }
      userScore = parseFloat(userInfo.userinfo.userScore) + userAddScore
      var userUpdateInfo = {
        userId: '',
        userName: '',
        userReconsider: '',
        userScore: '',
        userBranch: ''
      }
      // 更新信息
      userUpdateInfo.userId = userInfo.userId
      userUpdateInfo.userName = userInfo.userinfo.userName
      userUpdateInfo.userReconsider = '2'
      userUpdateInfo.userScore = userScore
      userUpdateInfo.userBranch = userBranch
      new Promise(function(resolve, reject) {
        updateUserInfo(userUpdateInfo).then(res => {
          resolve(res.msg)
        })
      }).then((data) => {
        if (data === '操作成功') {
          that.show = false
          that.$message({
            message: '修改成功',
            type: 'success'
          })
          top.location.reload()
        } else {
          that.$message({
            message: '服务器异常，请稍后重试',
            type: 'error'
          })
        }
      })
    },
    // 封禁用户
    updateUserStatus(user, userStatus) {
      var that = this
      this.$confirm('此操作将会导致该用户无法进入系统，请确定是否封禁', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateUserStatus(user, userStatus).then(res => {
          if (res.obj === null) {
            that.show = false
            that.$message({
              message: '封禁成功',
              type: 'success'
            })
            top.location.reload()
          } else {
            that.$message({
              message: '服务器异常，请稍后重试',
              type: 'success'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消封禁'
        })
      })
    },
    // 搜索页面信息
    searchUserInfomation() {
      var that = this
      var searchUserInfo = this.searchUserInfo
      if (searchUserInfo === '' || searchUserInfo === null || searchUserInfo === undefined) {
        if (that.originUserInfo.length !== 0) {
          that.userInfo = that.originUserInfo
        }
      } else {
        if (that.searchUserInfoStatus !== 1) {
          that.originUserInfo = that.userInfo
        }
        new Promise(function(resolve, reject) {
          searchUserInfomations(searchUserInfo).then(res => {
            resolve(res.obj)
          })
        }).then((data) => {
          if (data.length !== 0) {
            var giveUserInfo = []
            that.searchUserInfoStatus = 1
            new Promise(function(resolve, reject) {
              if (that.searchUserInfoStatus !== 1) {
                for (var i = 0; i < data.length; i++) {
                  var newUserInfo = that.userInfo.filter(function(val) {
                    return val.userId === data[i].userId
                  })
                  giveUserInfo.push(newUserInfo[0])
                  resolve(giveUserInfo)
                }
              } else {
                for (var j = 0; j < data.length; j++) {
                  var newUserInfo1 = that.originUserInfo.filter(function(val) {
                    return val.userId === data[j].userId
                  })
                  giveUserInfo.push(newUserInfo1[0])
                  resolve(giveUserInfo)
                }
              }
            }).then((data) => {
              that.userInfo = data
            })
          } else if (data.length === 0) {
            this.$message({
              type: 'warning',
              message: '未搜索到数据'
            })
          } else {
            this.$message({
              type: 'warning',
              message: '服务器异常，请稍后再试'
            })
          }
        })
      }
    },
    // 页面逻辑方法
    handleClose() {
      this.dialogFormVisible = false
      this.is_show_judge = false
      this.send = []
      this.list = []
      this.fetchData()
    },
    // 选择党小组
    selectUserBranch(value, row, column) {
      return row.userinfo.userBranch === value
    },
    // 清空全部筛选
    cleanSearchUserInfomation() {
      var that = this
      this.$message({
        message: '重置成功',
        type: 'success'
      })
      if (that.originUserInfo.length !== 0) {
        that.userInfo = that.originUserInfo
      }
      this.searchUserInfo = ''
      this.$refs.userInfoTable.clearFilter()
    },
    handleSizeChange(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
    }

  }
}
</script>

<style scope>
#main{
  margin-left: 5px;
  margin-right: 5px;
  margin-bottom: 10rem;
}
#table-area{
  margin-left: 0.8rem;
  margin-right: 0.8rem;
}
.title-infomation{
  margin-top:3rem;
  margin-left: 4rem;
}
.table-info{
  margin-left: 7rem;
  margin-right: 10rem;
}
.table-infomation{
  width: 100rem;
}
.paging-info{

    margin-top: 2rem;
}
.reset-userType{
    margin-top: 2rem;
}
.search-info{
    display: flex;
  margin-top:3rem;
  justify-content: flex-end;
  margin-bottom: 1.5rem;
  margin-right: 3rem;
}
.addUser{
  margin-top:3rem;
  margin-bottom: 1.5rem;
}
.table-allinfomation{
  margin-left: 7rem;
  margin-right: 5rem;
}
</style>
